package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.*;
import com.jiawa.train.business.domain.Req.ConfirmOrderDoReq;
import com.jiawa.train.business.domain.Req.ConfirmOrderQueryReq;
import com.jiawa.train.business.domain.Req.ConfirmOrderTicketReq;
import com.jiawa.train.business.domain.resp.ConfirmOrderQueryResp;
import com.jiawa.train.business.enums.ConfirmOrderStatusEnum;
import com.jiawa.train.business.enums.SeatColEnum;
import com.jiawa.train.business.enums.SeatTypeEnum;
import com.jiawa.train.business.mapper.ConfirmOrderMapper;
import com.jiawa.train.business.mapper.DailyTrainSeatMapper;
import com.jiawa.train.business.mapper.DailyTrainTicketMapper;
import com.jiawa.train.business.mapper.cust.custDailyTrainTicketMapper;
import com.jiawa.train.common.context.LoginMemberContext;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class AfterConfirmOrderService {

    @Autowired
    private ConfirmOrderMapper confirmOrderMapper;
    @Autowired
    private DailyTrainSeatMapper dailyTrainSeatMapper;
    @Autowired
    private custDailyTrainTicketMapper custDailyTrainTicketMapper;


    public void save(ConfirmOrderDoReq confirmOrderDoReq) {
        ConfirmOrder confirmOrder = BeanUtil.copyProperties(confirmOrderDoReq, ConfirmOrder.class);

        if(ObjectUtil.isNull(confirmOrder.getId())){
            confirmOrder.setId(SnowUtil.getSnowflakeNextId());
            confirmOrder.setCreateTime(new Date());
            confirmOrder.setUpdateTime(new Date());
            confirmOrderMapper.insert(confirmOrder);
        }else {
            confirmOrder.setUpdateTime(new Date());
            confirmOrderMapper.updateByPrimaryKeySelective(confirmOrder);
        }
    }

    @Transactional
    public void afterConfirmOrder(DailyTrainTicket dailyTrainTicket,List<DailyTrainSeat> finalSeatList) {
        for (DailyTrainSeat dailyTrainSeat : finalSeatList) {
            DailyTrainSeat trainSeat = new DailyTrainSeat();
            trainSeat.setId(dailyTrainSeat.getId());
            trainSeat.setSell(dailyTrainSeat.getSell());
            trainSeat.setUpdateTime(new Date());
            dailyTrainSeatMapper.updateByPrimaryKeySelective(trainSeat);

            Integer startIndex=dailyTrainTicket.getStartIndex();
            Integer endIndex=dailyTrainTicket.getEndIndex();
            char[] chars = trainSeat.getSell().toCharArray();
            Integer maxStartIndex=endIndex-1;
            Integer minEndIndex=startIndex+1;
            Integer minStartIndex=0;
            for(int i=startIndex; i>=0; i--){
                char aChar = chars[i];
                if(aChar=='1'){
                    minStartIndex=i+1;
                    break;
                }
            }

            Integer maxEndIndex=trainSeat.getSell().length();
            for(int i=maxStartIndex; i<trainSeat.getSell().length(); i++){
                char aChar = chars[i];
                if(aChar=='1'){
                    minEndIndex=i;
                    break;
                }
            }

            custDailyTrainTicketMapper.updateCountBySell(trainSeat.getDate(),
                    trainSeat.getTrainCode(),
                    trainSeat.getSeatType(),
                    minStartIndex,
                    maxStartIndex,
                    minEndIndex,
                    maxEndIndex);
        }
    }

}